using System;
using System.Collections.Generic;
using SportsStore.Domain.Abstract;
using SportsStore.Domain.Enities;

namespace SportsStore.Domain.Concrete
{
    public class ProductRepository : IProductRepository
    {
        private readonly SportStoreEFDbContext _dbContext;

        public ProductRepository(SportStoreEFDbContext dbContext)
        {
            _dbContext = dbContext;
        }
        public IEnumerable<Product> Products {
            get { return _dbContext.Products; } 
        }

        public void SaveProduct(Product product)
        {
            if (product.ProductID==0)
            {
                _dbContext.Products.Add(product);
            }
            else
            {
                var p = _dbContext.Products.Find(product.ProductID);
                if (p!=null)
                {
                    p.Name = product.Name;
                    p.Category = product.Category;
                    p.Description = product.Description;
                    p.Price = product.Price;
                    p.ImageData = product.ImageData;
                    p.ImageMimeType = product.ImageMimeType;
                }
            }
            _dbContext.SaveChanges();
        }

        public Product DeleteProduct(int productId)
        {
            var entry = _dbContext.Products.Find(productId);
            if (entry!=null)
            {
                _dbContext.Products.Remove(entry);
            }
            _dbContext.SaveChanges();
            return entry;
        }
    }
}